//
//  QDDoctorDetailsController.m
//  simulateQuickDoctor
//
//  Created by 姚华杰 on 2017/3/23.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDDoctorDetailsController.h"
#import <UIImageView+WebCache.h>
#import "QDNetworkTool.h"
#import <SVProgressHUD.h>

@interface QDDoctorDetailsController () <UIScrollViewDelegate>


//头像
@property (weak, nonatomic) IBOutlet UIImageView *imgView;

//名字
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

//类别
@property (weak, nonatomic) IBOutlet UILabel *sortLabel;

//医院名字
@property (weak, nonatomic) IBOutlet UILabel *hospitalNameLabel;

//预约数
@property (weak, nonatomic) IBOutlet UILabel *bespeakNumLabel;

//鲜花数
@property (weak, nonatomic) IBOutlet UILabel *flowerNumLabel;

//锦旗数
@property (weak, nonatomic) IBOutlet UILabel *jinqiNumLabel;

//移动条
@property (weak, nonatomic) IBOutlet UIImageView *navView;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (nonatomic, weak) UILabel *leftLabel;

@property (nonatomic, weak) UILabel *rightLabel;

@property (nonatomic, assign)BOOL flg;

@end

@implementation QDDoctorDetailsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [self loadXingxing];
}

- (void)loadXingxing
{
    
     UIImage *img = [UIImage new];
    
    if(_flg == 0)
    {
        img = [UIImage imageNamed:@"saveTemplateEmpty"];
        
    }
    else
    {
        img = [UIImage imageNamed:@"saveTemplate"];
        
     }
    
    UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(clickBarBtn)];
    
    self.navigationItem.rightBarButtonItem = barBtn;

}

- (void)setupUI
{
    
    
    self.navigationItem.title = @"医生详情";
    
    
    //加载图片
    UIImage *image = [UIImage imageNamed:@"doctor_defaultphoto_male"];
    
    //获取图片尺寸
    CGSize size = image.size;
    
    //开启位图上下文
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    
    //创建圆形路径
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    
    //设置为裁剪区域
    [path addClip];
    
    //绘制图片
    [image drawAtPoint:CGPointZero];
    
    //获取裁剪后的图片
    _imgView.image = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭上下文
    UIGraphicsEndImageContext();
    
    //添加左边label
    UILabel *leftLabel = [[UILabel alloc]init];
    [_scrollView addSubview:leftLabel];
    
    leftLabel.text = @"ohddhdkjhdkjdhadjhdaiu";
    
    [leftLabel setTextAlignment:NSTextAlignmentJustified];
    
    [leftLabel setBaselineAdjustment:UIBaselineAdjustmentNone];
    leftLabel.tag = 1;
    leftLabel.numberOfLines = 0;
    _leftLabel = leftLabel;
    
    //添加右边label
    UILabel *rightLabel = [[UILabel alloc]init];
    [_scrollView addSubview:rightLabel];
    rightLabel.text = @"djkhdjkgajdgdaga";
    rightLabel.tag = 2;
    rightLabel.numberOfLines = 0;
    _rightLabel = rightLabel;
    
    CGFloat width = _scrollView.bounds.size.width;
    CGFloat height = _scrollView.bounds.size.height;

    
    leftLabel.frame = CGRectMake(0, 0, width, height);
    
    rightLabel.frame = CGRectMake(width, 0, width, 30);
    
    _scrollView.bounces = NO;
    
    _scrollView.pagingEnabled = YES;
    
    _scrollView.contentSize = CGSizeMake(width*2, height);
    

    _scrollView.delegate = self;
    
 
    

}

- (void)clickBarBtn {
    
    _flg = !_flg;
    if(_flg)
    {
//        NSLog(@"关注成功");
        
        [SVProgressHUD showWithStatus:@"关注成功"];
        
        SVProgressHUD.defaultStyle = SVProgressHUDStyleDark;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
            //[self.navigationController popViewControllerAnimated:YES];
        });
        

    }
    else
    {
        NSLog(@"取消关注");
        
        [SVProgressHUD showWithStatus:@"取消关注"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
            
//            [self.navigationController popViewControllerAnimated:YES];
        });

    }
    
    
    [self loadXingxing];
    
}

- (IBAction)click:(UIButton *)sender {
    
    CGFloat width = _scrollView.bounds.size.width;
    
    if (sender.tag == 11) {
        
        [UIView animateWithDuration:0.5 animations:^{
            
            [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            _navView.frame = CGRectMake(0, 184, width/2, 3);
 
        }];
        
    }
    if (sender.tag == 12){
        
        [UIView animateWithDuration:0.5 animations:^{
            
            [_scrollView setContentOffset:CGPointMake(width, 0) animated:YES];
            
            _navView.frame = CGRectMake(width/2, 184, width/2, 3);
   
        }];
    }
}


#pragma mark - scrollView代理方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGFloat width = _scrollView.bounds.size.width;
    
    NSInteger index = _scrollView.contentOffset.x / width;
    
    
    NSLog(@"%zd, %zd",width, index);
    
    if (index == 0) {
        
        [UIView animateWithDuration:0.1 animations:^{
            _navView.frame = CGRectMake(0, 184, width/2, 3);
            
            
        }];
        
    }else{
        
        [UIView animateWithDuration:0.1 animations:^{
            
            _navView.frame = CGRectMake(width/2, 184, width/2, 3);
            
            
        }];
    }
}


- (void)loadData{
    
//    NSString *url = @"http://iosapi.itcast.cn/doctor/getDoctorInfo.json.php";
//    NSDictionary *parameters = @{@"user_id":@1000089,@"doctor_id":@300000315};
//   
//
//    QDNetworkTool *tool = [QDNetworkTool sharedTool];
//                           
//    tool.requestSerializer = [AFJSONRequestSerializer serializer];
//    
//    [[QDNetworkTool sharedTool] requestWithHTTPMethod:@"POST" URLString:url parameters:parameters CallBack:^(id responseObj) {
//        
//        NSLog(@"%@",responseObj);
        
//    }];
    
//    [_imgView sd_setImageWithURL:[NSURL URLWithString:_DAModel.doctor_portrait]];
    
    _nameLabel.text = _DAModel.doctor_name;
    
    _sortLabel.text = _DAModel.doctor_title_name;
    
//    _hospitalNameLabel.text = _DAModel.doctor_hospital_name;
    
    NSLog(@"%@",_hospitalNameLabel.text);
    NSString *str = [NSString stringWithFormat:@"预约数:%zd",_DAModel.operation_count];
    _bespeakNumLabel.text =  str;
    
    NSString *str2 = [NSString stringWithFormat:@"鲜花数:%zd",_DAModel.flower];
    _flowerNumLabel.text = str2;
    
    NSString *str3 = [NSString stringWithFormat:@"锦旗数:%zd",_DAModel.banner];
    _jinqiNumLabel.text = str3;

    
}

@end
